package ui;

import entity.User;
import exception.UserDisabledException;
import exception.UserNotFoundException;
import factory.ObjectFactory;
import service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginForm {
    private JPanel panel;
    private JLabel title;
    private JLabel usernameLabel;
    private JLabel passwordLabel;

    private JTextField usernameField;
    private JPasswordField passwordField;

    private JLabel messageLabel;

    private JButton loginButton;

    public LoginForm() {
        panel=new JPanel();
        title=new JLabel("用户登录");
        usernameLabel=new JLabel("账号：");
        passwordLabel=new JLabel("密码：");

        usernameField=new JTextField(20);
        passwordField=new JPasswordField(20);

        messageLabel=new JLabel(" ");

        loginButton=new JButton("登录");
    }

    private void init(){
        panel.setLayout(new BorderLayout(3,3));

        JPanel northPanel=new JPanel();
        northPanel.add(title);



        JPanel centerPanel=new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));

        JPanel usernamePanel=new JPanel();
        usernamePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        usernamePanel.add(Box.createRigidArea(new Dimension(10,0)));
        usernamePanel.add(usernameLabel);
        usernamePanel.add(usernameField);

        JPanel passwordPanel=new JPanel();
        passwordPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        passwordPanel.add(Box.createRigidArea(new Dimension(10,0)));
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);

        JPanel messagePanel=new JPanel();
        messagePanel.add(messageLabel);



        JPanel southPanel=new JPanel();
        southPanel.add(loginButton);

        centerPanel.add(Box.createRigidArea(new Dimension(0,50)));
        centerPanel.add(usernamePanel);
        centerPanel.add(passwordPanel);
        centerPanel.add(messagePanel);
        centerPanel.add(Box.createRigidArea(new Dimension(0,90)));


        panel.add(northPanel,BorderLayout.NORTH);
        panel.add(centerPanel,BorderLayout.CENTER);
        panel.add(southPanel,BorderLayout.SOUTH);


    }

    private void setStyle(){
        title.setFont(new Font("宋体",Font.BOLD,16));
        messageLabel.setForeground(Color.RED);
    }

    private void addEventHandler(){
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username=usernameField.getText();
                String password=new String(passwordField.getPassword());

                UserService userService= (UserService) ObjectFactory.getObject("userService");

                try {
                    User user=userService.login(username,password);

                    JOptionPane.getFrameForComponent(panel).dispose();
                    new SuccessFrame().show();

                } catch (UserNotFoundException ex) {
                    messageLabel.setText(ex.getMessage());
                } catch (UserDisabledException ex) {
                    messageLabel.setText(ex.getMessage());
                } catch (Exception ex){
                    messageLabel.setText("系统忙，稍后再试！");
                }
            }
        });
    }


    public void show(){
        init();
        setStyle();
        addEventHandler();
    }

    public JPanel getPanel() {
        return panel;
    }

}
